package sc.love.boot.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;

@Controller
public class UploadController {


    @GetMapping("MyUpload")
    public String MyUpload() {
        return "user/index";
    }


    @PostMapping("upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile multipartFile) {
        JSONObject jsonObject = new JSONObject();
        if (!multipartFile.isEmpty()) {
            String directory = System.getProperty("user.dir");
            String fileName = multipartFile.getOriginalFilename();
            File directoryFile = new File(directory);
            if (!directoryFile.isDirectory()) {
                directoryFile.mkdir();
            }
            File file = new File(directory + "/" + fileName);
            try {
                multipartFile.transferTo(file);
                jsonObject.put("code", "0");
                jsonObject.put("message", fileName);
            } catch (IOException e) {
                jsonObject.put("code", "1");
                jsonObject.put("message", e.getMessage());
                e.printStackTrace();
            }

        } else {
            jsonObject.put("code", "1");
            jsonObject.put("message", "失败");
        }
        return jsonObject.toJSONString();
    }


    @PostMapping("JSONPATH")
    @ResponseBody
    public String JSONPATH(String jPath, String file, String jParam) throws IOException {
        if ((file == null || "".equals(file)) && (jParam == null || "".equals(jParam) || "你可以上传文件也可以放在这里".equals(jParam))) {
            return "文件和输入框都没有值,这怎么给你转";
        }
        if (!"".equals(file) && !"".equals(jParam)) {
            return "文件和输入框都传了,哪知道是用哪个转";
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (file != null && !"".equals(file)) {
            String directory = System.getProperty("user.dir");
            File file1 = new File(directory + "/" + file);

            InputStreamReader streamReader = null;
            BufferedReader bufferedReader = null;
            if (file1.exists()) {
                //判断编码
                //  String coding = coding(file1);
                try {
                    streamReader = new InputStreamReader(new FileInputStream(file1));
                    bufferedReader = new BufferedReader(streamReader);
                    String len;
                    while ((len = bufferedReader.readLine()) != null) {
                        stringBuilder.append(len);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    bufferedReader.close();
                    streamReader.close();
                }
            }
        }

        if (stringBuilder.length() == 0) {
            stringBuilder.append(jParam);
        }
        Object eval = null;
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(stringBuilder.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return "老铁,json报文有问题,能不能好好复制";
        }
        try {
            eval = JSONPath.eval(jsonObject, jPath);
        } catch (Exception e) {
            e.printStackTrace();
            return "老铁,jpath写错了,能不能好好写";
        }

        if (eval != null) {
            if (eval instanceof JSONObject) {
                JSONObject eval1 = (JSONObject) eval;
                return eval1.toJSONString();
            }
            if (eval instanceof JSONArray) {
                return ((JSONArray) eval).toJSONString();
            }
            if (eval instanceof String) {
                return ((String) eval);
            }
            if (eval instanceof Integer) {
                return ((Integer) eval) + "";
            }
            if (eval instanceof Long) {
                return ((Long) eval) + "";
            }
            if (eval instanceof ArrayList) {
                return JSONObject.toJSONString(((ArrayList) eval));
            }
        }
        return "";
    }


    public String coding(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[3];
            try {
                fileInputStream.read(bytes);
                fileInputStream.close();
                if (bytes[0] == -17 && bytes[1] == -69 && bytes[2] == -65) {
                    return "UTF-8";
                } else if (bytes[0] == 34 && bytes[1] == 32 && bytes[2] == 73) {
                    return "Big5";
                } else {
                    return "GBK";
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return "UTF-8";
    }
}
